package com.chenglai.article.service.impl;

import com.chenglai.article.dao.ArticleCategoryMapper;
import com.chenglai.article.dao.ArticleInfoMapper;
import com.chenglai.article.pojo.ArticleCategory;
import com.chenglai.article.pojo.ArticleInfo;
import com.chenglai.article.service.ArticleCategoryService;
import com.chenglai.article.service.ArticleInfoService;
import com.chenglai.commonutils.common.DateFormatHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * 类描述：
 *
 * @Author rxh
 * @Date 2023/11/15 11:41
 */
@Service
public class ArticleInfoServiceImpl implements ArticleInfoService {

    @Resource
    private ArticleCategoryService articleCategoryService;

    @Resource
    private ArticleInfoMapper articleInfoMapper;


    @Override
    public PageInfo getArticleInfoList(Integer page, Integer size, Integer shopId) {
        PageHelper.startPage(page, size);
        Example example = new Example(ArticleInfo.class);
        Example.Criteria criteria = example.createCriteria();
        if (shopId != 0) {
            criteria.andEqualTo("shopId", shopId);
        }
        example.orderBy("articleSort").desc();
        return new PageInfo(articleInfoMapper.selectByExample(example));
    }

    @Override
    public void editArticleInfo(ArticleInfo articleInfo) {
        if (articleInfo.getArticleId() == null) {
            //添加
            articleInfo.setIsDelete(1);
            articleInfo.setCreateTime(DateFormatHelper.currentTimes());
            articleInfoMapper.insertSelective(articleInfo);
        } else {
            //修改
            articleInfoMapper.updateByPrimaryKeySelective(articleInfo);
        }

    }

    @Override
    public void delArticleInfo(Integer articleId) {
        articleInfoMapper.deleteByPrimaryKey(articleId);
    }

    @Override
    public ArticleInfo detail(Integer articleId) {
        return articleInfoMapper.selectByPrimaryKey(articleId);
    }

    @Override
    public void updateSortByArticleId(Integer articleId, Integer sort) {
        ArticleInfo articleInfo = new ArticleInfo();
        articleInfo.setArticleId(articleId);
        articleInfo.setArticleSort(sort);
        articleInfoMapper.updateByPrimaryKeySelective(articleInfo);
    }

    @Override
    public void updateIsDeleteByArticleId(Integer articleId, Integer isDelete) {
        ArticleInfo articleInfo = new ArticleInfo();
        articleInfo.setArticleId(articleId);
        articleInfo.setIsDelete(isDelete);
        articleInfoMapper.updateByPrimaryKeySelective(articleInfo);
    }

    @Override
    public List<ArticleCategory> articleAll(Integer shopId) {
        //查询分类
        List<ArticleCategory> list = articleCategoryService.getArticleCateGoryList(shopId);
        list.stream().forEach(map -> map.setArticleInfoList(
                articleCategoryService.queryArticleListByCategoryId(map.getCategoryId())
        ));
        return list;
    }
}
